# -*- coding: utf-8 -*-
# @Time    : 2025/1/23 上午5:54
# @Author  : lh
# @Email   : lhhlhh822@peatlmail.com
# @File    : add_announcement_page.py
# @Software: PyCharm
import os

from selenium.webdriver import Keys
from selenium.webdriver.common.alert import Alert

from config.config import DATAS_PATH
from pages.base_page import BasePage
from selenium.webdriver.common.by import By


class AddAnnouncement(BasePage):
    url = "/crm/index.php?m=announcement&a=add"
    # 定位器
    title_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div > form > table > tbody > tr:nth-child(2) > '
                                      'td:nth-child(2) > input')  # 标题
    color_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div > form > table > tbody > tr:nth-child(3) > '
                                      'td:nth-child(2) > input')  # 标题颜色
    content_locator = (
        By.CSS_SELECTOR, 'body > div.container > div.row > div > form > table > tbody > tr:nth-child(7) > '
                         'td:nth-child(2) > div > div.ke-edit > iframe')  # 内容
    save_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/form/table/tfoot/tr/td/input[1]')  # 保存
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')

    #输入公告标题
    def input_title(self, title):
        self.find_element(self.title_locator).send_keys(title)

    #输入颜色
    def set_title_color(self, color):
        color_element = self.find_element(self.color_locator)  # 定位颜色输入框
        color_element.send_keys(Keys.CONTROL, "a")
        color_element.send_keys(color)

    #输入公告内容
    def input_content(self, content):
        self.find_element(self.content_locator).send_keys(content)

    def click_save_btn(self):
        self.find_element(self.save_locator).click()



    # 组装数据文件的绝对路径
    def get_abs_file(self, filename):
        return os.path.join(DATAS_PATH, filename)

    # 实例化Alert对象
    def _alert(self):
        return Alert(self.driver)

    # 确认警告框
    def alert_accept(self):
        self._alert().accept()

    # 取消弹框
    def alert_dismiss(self):
        self._alert().dismiss()

    # 返回增删改查后的提示信息
    def get_result_tips(self):
        return self.find_element(self.result_locator).text

    # 返回弹窗文本信息
    def get_alert_text(self):
        return self.driver.switch_to.alert.text
